package cn.laike.modules.dev.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.Query;
import cn.laike.modules.dev.dao.MemberDao;
import cn.laike.modules.dev.entity.MemberEntity;
import cn.laike.modules.dev.service.MemberService;
import cn.laike.modules.sys.shiro.ShiroUtils;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String mobile = (String)params.get("mobile");
		String name = (String)params.get("name");
		String number = (String)params.get("number");
    	
        Page<MemberEntity> page = this.selectPage(
                new Query<MemberEntity>(params).getPage(),
                new EntityWrapper<MemberEntity>()
                .eq(ShiroUtils.isAdmin(),"uid", ShiroUtils.getUserId())
	            .eq(StringUtils.isNotBlank(mobile),"mobile", mobile)
	            .eq(StringUtils.isNotBlank(name),"name", name)
	            .like(StringUtils.isNotBlank(number),"number", number)
	            .orderBy("id", false)
        );

        return new PageUtils(page);
    }
    
	@Override
	public MemberEntity getMemberByMobile(Long uid,String mobile) {
		List<MemberEntity> list = this.selectList(new EntityWrapper<MemberEntity>()
				.eq("uid", uid)
    			.eq("mobile", mobile)
    			);
    	return (list != null && list.size() > 0) ? list.get(0) : null;
	}

	@Override
	public MemberEntity getMemberByOpenid(String openid) {
		List<MemberEntity> list = this.selectList(new EntityWrapper<MemberEntity>()
    			.eq("openid", openid));
    	return (list != null && list.size() > 0) ? list.get(0) : null;
	}

}
